package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.*;
import tech.aistar.service.IAgentService;
import tech.aistar.service.IHouseService;
import tech.aistar.service.ILandlordService;
import tech.aistar.service.IRegisterService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Landlord")
public class LandlordCentre {
    @Autowired
    private ILandlordService landlordService;

    @Autowired
    private IRegisterService registerService;

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IAgentService agentService;

    @GetMapping("/info")
    public String select(HttpServletRequest request, Integer id) {
        String landlordId = request.getParameter("landlordId");
        Landlord landlord = landlordService.selectById(Integer.valueOf(landlordId));
        request.setAttribute("landlord", landlord);
        Register register = registerService.selectById(landlord.getRegisterId());
        request.setAttribute("register", register);
        return "/personal_homepage/landlord/info";
    }


    @PostMapping("/changepwd")
    public @ResponseBody
    Result updatepwd(Integer id, String password) {
        int count = registerService.updateById(id, password);

        if (count == 1) {
            return new Result("200", "修改成功", registerService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }



    @PostMapping("/changephone")
    public @ResponseBody Result updatephone(String phone, Integer id) {
        int count = landlordService.updateById(phone, id);

        if (count == 1) {
            Result result = new Result("200", "修改成功", landlordService.selectById(id));
            return result;
        } else {
            Result result = new Result("500", "修改失败");
            return result;
        }
    }

    @PostMapping("/changename")
    public @ResponseBody Result updatename(String name, Integer id) {

        int count = landlordService.updateNameById(name, id);

        if (count == 1) {
            return new Result("200", "修改成功", landlordService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @PostMapping("/changegender")
    public @ResponseBody Result updategender(String gender, Integer id) {

        int count = landlordService.updategenderById(gender, id);

        if (count == 1) {
            return new Result("200", "修改成功", landlordService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @GetMapping("/PutOut")
    public String PutOut(HttpServletRequest request) {
        String landlordId = request.getParameter("landlordId");
        Landlord landlord = landlordService.selectById(Integer.valueOf(landlordId));
        request.setAttribute("landlord", landlord);
        return "/personal_homepage/landlord/house";
    }



    @GetMapping("/HouseAgent")
    public String HouseAgent(HttpServletRequest request) {
        String landlordId = request.getParameter("landlordId");
        Landlord landlord = landlordService.selectById(Integer.valueOf(landlordId));
        request.setAttribute("landlord", landlord);
        List<House> houseList = houseService.selectBylandlordId(Integer.valueOf(landlordId));
        request.setAttribute("houseList",houseList);
        List<Agent> agentList = new ArrayList<>();
        for (int i = 0; i <houseList.size() ; i++) {
            Agent agent = agentService.selectById(houseList.get(i).getAgentId());
            agentList.add(agent);
        }
        request.setAttribute("agentList",agentList);
        return "/personal_homepage/landlord/myhouse";
    }


    @PostMapping("/delHouse")
    public @ResponseBody Result delhouse(Integer id) {
        int count = houseService.deleteById(id);
        if (count==1){
            return new Result("200","成功取消");
        }else{
            return new Result("500","取消失败");
        }
    }








}
